﻿using System;
using System.Gadgets;

namespace MythtvGadget
{
    internal class Settings
    {
        private const string SETTINGS_URL = "MythStatusUrl";
        private const string MYTHWEB_URL = "MythwebUrl";
        private const string SETTINGS_INTERVAL = "UpdateInterval";
        private const string GUIDE_DAYS_WARNING = "GuideWarning";
		private const string GADGET_HEIGHT = "GadgetHeight";
		private const string DATE_FORMAT = "DateFormat";
		private const string DATE_ON_NEWLINE = "DateOnNewLine";

		public const int DEFAULT_HEIGHT = 225;
		public const string DEFAULT_DATE_FORMAT = "dd MMM, HH:mm";

        public static string StatusUrl
        {
            get
            {
                return GetUrlWithTerminatingSlash(SETTINGS_URL);
            }
            set
            {
                Gadget.Settings.WriteString(SETTINGS_URL, value);
            }
        }

        public static string MythwebUrl
        {
            get
            {
                return GetUrlWithTerminatingSlash(MYTHWEB_URL);
            }
            set
            {
                Gadget.Settings.WriteString(MYTHWEB_URL, value);
            }
        }

        private static string GetUrlWithTerminatingSlash(string settingName)
        {
            string url = Gadget.Settings.ReadString(settingName);

            if (!url.EndsWith('/') && !string.IsNullOrEmpty(url))
                url += "/";

            return url;
        }

        public static int UpdateInterval
        {
            get
            {
                string interval = Gadget.Settings.ReadString(SETTINGS_INTERVAL);

                if (String.IsNullOrEmpty(interval))
                    return 300;
                else
                    return (int)int.Parse(interval);
            }
            set
            {
                Gadget.Settings.WriteString(SETTINGS_INTERVAL, value.ToString());
            }
        }

        public static int GuideWarningDays
        {
            get
            {
                string days = Gadget.Settings.ReadString(GUIDE_DAYS_WARNING);

                if (String.IsNullOrEmpty(days))
                    return 7;
                else
                    return (int)int.Parse(days);
            }
            set
            {
                Gadget.Settings.WriteString(GUIDE_DAYS_WARNING, value.ToString());
            }
        }

		public static int GadgetHeight
		{
			get
			{
				int height = (int)Gadget.Settings.Read(GADGET_HEIGHT);
				if (height == 0)
					height = DEFAULT_HEIGHT;
				return height;
			}
			set
			{
				Gadget.Settings.Write(GADGET_HEIGHT, value);
			}
		}

		public static string DateFormat
		{
			get 
			{
				string dateFormat = Gadget.Settings.ReadString(DATE_FORMAT);
				if (string.IsNullOrEmpty(dateFormat))
					dateFormat = DEFAULT_DATE_FORMAT;
				return dateFormat;
			}
			set
			{
				Gadget.Settings.WriteString(DATE_FORMAT, value);
			}
		}

		public static bool DateOnNewLine
		{
			get
			{
				return (bool)bool.Parse(Gadget.Settings.ReadString(DATE_ON_NEWLINE));
			}
			set
			{
				Gadget.Settings.WriteString(DATE_ON_NEWLINE, value.ToString());
			}
		}
    }
}
